package core.file;

import java.awt.image.BufferedImage;
import java.io.*;
import enviro.*;

import javax.imageio.ImageIO;
final class FileReader {
    
    
    //<editor-fold defaultstate="collapsed" desc="public BufferedImage readImage(String filename)">
    /**
     * Read an image into memory
     * 
     * @param filename
     *   The path location of the image.
     * @return
     *   A BufferedImage object derived from the file read.
     * 
     * @throws IOException 
     *   Thrown if there was a problem during the read operation.
     */
	public BufferedImage readImage(String filename) throws IOException {
		
		// Initiate the filestream and the associated image file
		File file = new File(filename);
		BufferedImage bi = ImageIO.read(file);
		return bi;
	}
      //</editor-fold>
        
    //<editor-fold defaultstate="collapsed" desc="public static Object readObject(String path)">
        /**
         * Returns an object from the file at the specified path
         * @param path
         *   This is the path where the file is to be found
         * @return
         *   The object read from the file.
         * @throws FileNotFoundException
         * @throws IOException
         * @throws ClassNotFoundException 
         */
      public static Object readObject(String path) throws FileNotFoundException, 
              IOException, ClassNotFoundException {
          
          // Capture the file to read from
          FileInputStream fis = new FileInputStream(path);
          ObjectInputStream ois = new ObjectInputStream(fis);
          
          Object object = new Object(); // will capture the object
          
          // Capture object
          try {
            object = ois.readObject();
          }
          catch (FileNotFoundException ex) {
              ex.printStackTrace();
             System.out.println(ex.getMessage());
          }
          catch (IOException ex) {
              ex.printStackTrace();
             System.out.println(ex.getMessage());
          }
          catch (ClassNotFoundException ex) {
              ex.printStackTrace();
             System.out.println(ex.getMessage());
          }
          
          // Close streams
          ois.close();
          fis.close();
          
          // Return the object
          return object;
      }
     //</editor-fold>
	
}
